<pre class="metadata">
Title: Gyroscope
Status: ED
Level: none
ED: https://w3c.github.io/gyroscope/
Shortname: gyroscope
TR: https://www.w3.org/TR/gyroscope/
Previous Version: https://www.w3.org/TR/2019/WD-gyroscope-20190307/
Editor: Anssi Kostiainen 41974, Intel Corporation, http://intel.com/
Former Editor: Mikhail Pozdnyakov 78325, Intel Corporation, http://intel.com/
Group: dap
Abstract:
  This specification defines a concrete sensor interface to monitor
  the rate of rotation around the device's local three primary axes.
Version History: https://github.com/w3c/gyroscope/commits/master/index.bs
!Bug Reports: <a href="https://www.github.com/w3c/gyroscope/issues/new">via the w3c/gyroscope repository on GitHub</a>
Indent: 2
Repository: w3c/gyroscope
Markup Shorthands: markdown on
Inline Github Issues: true
!Test Suite: <a href="https://github.com/web-platform-tests/wpt/tree/master/gyroscope">web-platform-tests on GitHub</a>
Boilerplate: omit issues-index, omit conformance
Default Biblio Status: current
</pre>
<pre class="anchors">
urlPrefix: https://w3c.github.io/sensors/; spec: GENERIC-SENSOR
  type: dfn
    text: high-level
    text: latest reading
    text: default sensor
    text: sensor type
    text: local coordinate system
    text: sensor reading
    text: sensor permission name; url: sensor-permission-names
    text: location tracking; url: location-tracking
    text: keylogging; url: keystroke-monitoring
    text: fingerprinting; url: device-fingerprinting
    text: user identifying; url: user-identifying
    text: eavesdropping; url: eavesdropping
    text: generic mitigations; url: mitigation-strategies
    text: supported sensor options
    text: automation
    text: mock sensor type
    text: mock sensor reading values
urlPrefix: https://w3c.github.io/accelerometer/; spec: ACCELEROMETER
  type: dfn
    text: device coordinate system
    text: screen coordinate system
</pre>

<pre class=biblio>
{
    "KEYSTROKEDEFENSE": {
        "authors": [
            "Song, Yihang, et al"
        ],
        "id": "KEYSTROKEDEFENSE",
        "href": "https://arxiv.org/abs/1410.7746",
        "title": "Two novel defenses against motion-based keystroke inference attacks",
        "date": "2014",
        "status": "Informational",
        "publisher": "arXiv"
     },
    "TOUCHSIGNATURES": {
        "authors": [
            "Mehrnezhad, Maryam, et al"
        ],
        "id": "TOUCHSIGNATURES",
        "href": "https://arxiv.org/abs/1602.04115",
        "title": "Touchsignatures: identification of user touch actions and pins based on mobile sensor data via javascript",
        "date": "2016",
        "status": "Informational",
        "publisher": "Journal of Information Security and Applications"
     }
}
</pre>

Introduction {#intro}
============

The Gyroscope API extends the Generic Sensor API [[GENERIC-SENSOR]]
to provide information about the angular velocity around the device's local X, Y and Z axis
in terms of radian per seconds units.

Use Cases and Requirements {#usecases-requirements}
==============================

The use cases and requirements are addressed in the <cite><a href="https://w3c.github.io/motion-sensors/#usecases-and-requirements">
Motion Sensors Explainer</a></cite> document.

Examples {#examples}
========

<div class="example">
    <pre highlight="js">
    let sensor = new Gyroscope();
    sensor.start();

    sensor.onreading = () => {
        console.log("Angular velocity around the X-axis " + sensor.x);
        console.log("Angular velocity around the Y-axis " + sensor.y);
        console.log("Angular velocity around the Z-axis " + sensor.z);
    };

    sensor.onerror = event => console.log(event.error.name, event.error.message);
    </pre>
</div>

Security and Privacy Considerations {#security-and-privacy}
===================================

[=Sensor readings=] provided by inertial sensors, such as gyroscope, could be used by adversaries
to exploit various security threats, for example, [=keylogging=], [=location tracking=],
[=fingerprinting=], [=user identifying=] and even [=eavesdropping=].

Research papers published by security community, for instance, [[KEYSTROKEDEFENSE]], indicate that
by throttling the frequency, risks of successful attacks are not fully eliminated, while throttling
may greatly affect usefulness of a web application with legitimate reasons to use the sensors.

The [[TOUCHSIGNATURES]] research paper proposes that implementations can provide visual indication when
inertial sensors are in use and/or require explicit user consent to access [=sensor readings=].
These mitigation strategies complement the [=generic mitigations=] defined
in the Generic Sensor API [[!GENERIC-SENSOR]].

Model {#model}
=====

The <dfn id="gyroscope-sensor-type">Gyroscope</dfn> <a>sensor type</a>'s associated {{Sensor}} subclass is the {{Gyroscope}} class.

The <a>Gyroscope</a> has a <a>default sensor</a>, which is the device's main gyroscope sensor.

The <a>Gyroscope</a> has an associated [=sensor permission name=] which is <a for="PermissionName" enum-value>"gyroscope"</a>.

A [=latest reading=] of a {{Sensor}} of <a>Gyroscope</a> <a>sensor type</a> includes three [=map/entries=]
whose [=map/keys=] are "x", "y", "z" and whose [=map/values=] contain current <a>angular
velocity</a> about the corresponding axes.

The <dfn>angular velocity</dfn> is the rate at which the device rotates
about a specified axis in a [=local coordinate system=] defined by the device.
Its unit is the radian per second (rad/s) [[SI]].

The sign of the current <a>angular velocity</a> depends on the rotation direction and
it must be according to the right-hand convention in a [=local coordinate system=]
defined by the device, such that positive rotation around an axis is clockwise when
viewed along the positive direction of the axis (see figure below).

<img src="images/gyroscope_sensor_coordinate_system.png" srcset="images/gyroscope_sensor_coordinate_system.svg" alt="Device's local coordinate system and rotation.">


Reference Frame {#reference-frame}
----------------

The [=local coordinate system=] represents the reference frame for the
{{Gyroscope}} [=sensor readings|readings=]. It can be either
the [=device coordinate system=] or the [=screen coordinate system=].

API {#api}
===

The Gyroscope Interface {#gyroscope-interface}
------------------------

<pre class="idl">
  [SecureContext, Exposed=Window]
  interface Gyroscope : Sensor {
    constructor(optional GyroscopeSensorOptions sensorOptions = {});
    readonly attribute double? x;
    readonly attribute double? y;
    readonly attribute double? z;
  };

  enum GyroscopeLocalCoordinateSystem { "device", "screen" };

  dictionary GyroscopeSensorOptions : SensorOptions {
    GyroscopeLocalCoordinateSystem referenceFrame = "device";
  };
</pre>

To construct a {{Gyroscope}} object the user agent must invoke
the [=construct a gyroscope object=] abstract operation.

[=Supported sensor options=] for {{Gyroscope}} are
"frequency" and "referenceFrame".

### Gyroscope.x ### {#gyroscope-x}

The {{Gyroscope/x!!attribute}} attribute of the {{Gyroscope}}
interface represents the current <a>angular velocity</a> around X-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with <emu-val>this</emu-val>
and "x" as arguments.

### Gyroscope.y ### {#gyroscope-y}

The {{Gyroscope/y!!attribute}} attribute of the {{Gyroscope}}
interface represents the current <a>angular velocity</a> around Y-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with <emu-val>this</emu-val>
and "y" as arguments.

### Gyroscope.z ### {#gyroscope-z}

The {{Gyroscope/z!!attribute}} attribute of the {{Gyroscope}}
interface represents the current <a>angular velocity</a> around Z-axis.
In other words, this attribute returns the result of invoking
[=get value from latest reading=] with <emu-val>this</emu-val>
and "z" as arguments.

Abstract Operations {#abstract-operations}
===================

<h3 dfn export>Construct a Gyroscope object</h3>

<div algorithm="construct a gyroscope object">

    : input
    :: |options|, a {{GyroscopeSensorOptions}} object.
    : output
    :: A {{Gyroscope}} object.

    1.  Let |allowed| be the result of invoking [=check sensor policy-controlled features=]
        with <a>Gyroscope</a>.
    1.  If |allowed| is false, then:
        1.  [=Throw=] a {{SecurityError}} {{DOMException}}.
    1.  Let |gyroscope| be the new {{Gyroscope}} object.
    1.  Invoke [=initialize a sensor object=] with |gyroscope| and |options|.
    1.  If |options|.{{referenceFrame!!dict-member}} is "screen", then:
        1.  Define [=local coordinate system=] for |gyroscope|
            as the [=screen coordinate system=].
    1.  Otherwise, define [=local coordinate system=] for |gyroscope|
        as the [=device coordinate system=].
    1.  Return |gyroscope|.
</div>

Automation {#automation}
==========
This section extends the [=automation=] section defined in the Generic Sensor API [[GENERIC-SENSOR]]
to provide mocking information about the rate of rotation around the device's local three primary axes
for the purposes of testing a user agent's implementation of {{Gyroscope}} API.

<h3 id="mock-gyroscope-type">Mock Sensor Type</h3>

The {{Gyroscope}} class has an associated [=mock sensor type=] which is
<a for="MockSensorType" enum-value>"gyroscope"</a>, its [=mock sensor reading values=]
dictionary is defined as follows:

<pre class="idl">
  dictionary GyroscopeReadingValues {
    required double? x;
    required double? y;
    required double? z;
  };
</pre>

Acknowledgements {#acknowledgements}
================

Tobie Langel for the work on Generic Sensor API.

Conformance {#conformance}
===========

Conformance requirements are expressed with a combination of
descriptive assertions and RFC 2119 terminology. The key words "MUST",
"MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT",
"RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this
document are to be interpreted as described in RFC 2119.
However, for readability, these words do not appear in all uppercase
letters in this specification.

All of the text of this specification is normative except sections
explicitly marked as non-normative, examples, and notes. [[!RFC2119]]

A <dfn>conformant user agent</dfn> must implement all the requirements
listed in this specification that are applicable to user agents.

The IDL fragments in this specification must be interpreted as required for
conforming IDL fragments, as described in the Web IDL specification. [[!WEBIDL]]
